<?php
namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Config;
use think\Request;
use app\index\model\User;
use app\index\model\ss;
use app\index\model\sc;

class Classes extends Controller{
    public function index(){
        $hint = '';
        $this->assign('hint',$hint);

        if(Request::instance()->param()){
            $has = Request::instance()->param();
            // dump($has);
            $class_Id = $has["class_Id"];
        }else{
            $class_Id = Session::get("class_Id");
        }
        Session::set('class_Id',$class_Id);

        $studentlist = new User();
        $student = $studentlist->clastu($class_Id);
        // dump($student->toArray());
        $this->assign('studentlist',$student);

        return $this->fetch();
    }

    public function addstudent(){
        $hint = Session::get('Hint');
        $this->assign('hint',$hint);

        return $this->fetch();
    }

    public function doaddstudent(){
        if(Request::instance()->isPost()){
            $has = Request::instance()->post();
            //var_dump($has);
            $class_Id = Session::get("class_Id");
            $Id = $has["Id"];

            $model = new User();
            $student = $model->newstudent($Id);
            //dump($student);
            
            if(empty($student)){
                $hint = '该学生不存在';
                Session::set('Hint',$hint);
                $this->redirect('index/Classes/addstudent');
            }else{
                $student_Id = $student->toArray()["Id"];
                $addss = new ss();
                $ad = $addss->addstudent($class_Id,$student_Id,1);
                $courseId = Session::get("courseId");
                $addsc = new sc();
                $add = $addsc->joincourse($courseId,$student_Id);
                if($ad === false){
                    $hint = '学生添加失败';
                    Session::set('Hint',$hint);
                    $this->redirect('index/Classes/addstudent');
                }else{
                    $this->redirect('index/Classes/index');
                }
            }
        } 
    }

    //状态变更
    public function setStatus(){                  //从前端的jQuery调用控制器
        Config::set("default_return_type","json");//设置返回的类型为json类型

        $requset = Request::instance();           //拿到前端的传入数据
        if($requset->isAjax()){                   //判断数据是否通过ajax传过来
            $status = $requset->post("status");
            $ids = $requset->post("ids");
            $userModel = new ss();
            $res = $userModel->setStatus($status,$ids);
            if($res === false){
                return array("flag"=>0,"msg"=>"更改状态失败");
            }
            return array("flag"=>1,"msg"=>"成功");
        }else{
            return array("flag"=>0,"msg"=>"非Ajax访问");
        }
       
    }
}
?>